<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${MODEL.nameSpance}">
	<!--sql段所有字段名 -->
	<sql id="columns">
	<#list MODEL.property as item> 
			<#if item_has_next>
			${item.COLUMN_NAME},
			<#else>
			${item.COLUMN_NAME}
			</#if>
		</#list>	
	</sql>
	
	<!--sql段所有属性名 -->
	<sql id="values">
	<#list MODEL.property as item>
		<#if item_has_next>
			${'#'}{${item.PROPERTY}},
			<#else>
			${'#'}{${item.PROPERTY}}
		</#if>
	</#list>	
	 
	</sql>
	<!--sql段set -->
	<sql id="set">
	<!--不遍历id -->
	<#list MODEL.property as item>
		<#if item_index!=0>
		<#if item_has_next>
		<if test="(${item.PROPERTY}!=null and ${item.PROPERTY}!='')">
			${item.COLUMN_NAME} = ${'#'}{${item.PROPERTY}}, 
		</if>
		<#else>
		<if test="(${item.PROPERTY}!=null)">
			${item.COLUMN_NAME} = ${'#'}{${item.PROPERTY}}
		</if>
		</#if>
		</#if>
	</#list>	
		
	</sql>

	<sql id="where">
	<if test="record!=null">
		<#list MODEL.property as item>
		<if test="(record.${item.PROPERTY}!=null and record.${item.PROPERTY}!='')">
			and	${item.COLUMN_NAME} = ${'#'}{record.${item.PROPERTY}} 
		</if>
	</#list>
	</if>	
	</sql>
	
	<!--map关联-->
	<resultMap type="${MODEL.beanName}" id="${MODEL.beanName}Map">
	<#list MODEL.property as item>
	<#if item_index==0>
	<id property="${item.COLUMN_NAME}" column="${item.PROPERTY}" />
	<#else>
	<result property="${item.COLUMN_NAME}" column="${item.PROPERTY}" />	
	</#if>
	</#list>	
	</resultMap>

	<!--插入-->
	<insert id="insert" parameterType="${MODEL.beanName}" useGeneratedKeys="true"
		keyProperty="id">
		INSERT INTO ${MODEL.tableName} (
		<#list MODEL.property as item>
		<#if item_index!=0>
		<#if item_has_next>
			${item.COLUMN_NAME} , 
		<#else>
			${item.COLUMN_NAME} 
		</#if>
		</#if>
		</#list>	
		)
		VALUES (
		<#list MODEL.property as item>
		<#if item_index!=0>
		<#if item_has_next>
			${'#'}{${item.PROPERTY}} , 
		<#else>
			${'#'}{${item.PROPERTY}} 
		</#if>
		</#if>
		</#list>	
		)
	</insert>
	
	
	<!-- 根据id更新 部分更新 -->
	<update id="update" parameterType="java.lang.Integer">
		UPDATE ${MODEL.tableName}
		<set>
			<include refid="set" />
		</set>
		WHERE id=${'#'}{id}
	</update>


	<!-- 批量删除 -->
	<delete id="deleteByIds" parameterType="java.util.List">
		delete from ${MODEL.tableName} where id in
		<foreach collection="list" index="index" item="item" open="("
			separator="," close=")">
			${'#'}{item}
		</foreach>
	</delete>

	<!-- 根据id删除 -->
	<delete id="deleteById" parameterType="java.lang.Integer">
		DELETE FROM ${MODEL.tableName}
		where
		id=${'#'}{id}
	</delete>

	<!-- 根据对象删除 -->
	<delete id="deleteByModel" parameterType="java.lang.Integer">
		DELETE FROM ${MODEL.tableName}
		where 1=1
		<include refid="where" />
	</delete>

	<!-- 分页查询 -->
	<select id="selectInPage" resultMap="${MODEL.beanName}Map">
		 select top ${'$'}{page.limit-page.offset } * from (SELECT ROW_NUMBER() 
		OVER (ORDER BY id) AS Rownum,* from ${MODEL.tableName} where 1=1
		<include refid="where" />
		)a
		where Rownum > ${'#'}{page.offset }
	</select>

	<!-- 查询总条数 -->
	<select id="selectTotalRecord"  resultType="java.lang.Integer">
		select count(*)
		from ${MODEL.tableName}
		where 1=1
		<include refid="where" />
	</select>

	<!-- 根据id查询 -->
	<select id="selectById" resultMap="${MODEL.beanName}Map">
		select
		<include refid="columns" />
		from ${MODEL.tableName}
		where id=${'#'}{id}
	</select>


	<!-- 根据对象查询 -->
	<select id="selectByModel" resultMap="${MODEL.beanName}Map" parameterType="${MODEL.beanName}">
		select
		<include refid="columns" />
		from ${MODEL.tableName}
		where 1=1
		<include refid="where" />
	</select>
	
	
</mapper>